package com.zscat.mallplus.sys.controller;


import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.sys.entity.AdminSysNotice;
import com.zscat.mallplus.sys.service.IAdminSysNoticeService;
import com.zscat.mallplus.util.EasyPoiUtils;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author mallplus
 * @date 2020-06-10
 * 商城通知
 */
@Slf4j
@RestController
@RequestMapping("/sys/adminSysNotice")
public class AdminSysNoticeController {

    @Resource
    private IAdminSysNoticeService IAdminSysNoticeService;

    @SysLog(MODULE = "sys", REMARK = "根据条件查询所有商城通知列表")
    @ApiOperation("根据条件查询所有商城通知列表")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAuthority('sys:adminSysNotice:read')")
    public Object getAdminSysNoticeByPage(AdminSysNotice entity,
                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        try {
            return new CommonResult().success(IAdminSysNoticeService.page(new Page<AdminSysNotice>(pageNum, pageSize), new QueryWrapper<>(entity).orderByDesc("create_time")));
        } catch (Exception e) {
            log.error("根据条件查询所有商城通知列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "保存商城通知")
    @ApiOperation("保存商城通知")
    @PostMapping(value = "/create")
    @PreAuthorize("hasAuthority('sys:adminSysNotice:create')")
    public Object saveAdminSysNotice(@RequestBody AdminSysNotice entity) {
        try {
            entity.setCreateTime(new Date());
            if (IAdminSysNoticeService.save(entity)) {
                return new CommonResult().success(entity);
            }
        } catch (Exception e) {
            log.error("保存商城通知：%s", e.getMessage(), e);
            return new CommonResult().failed(e.getMessage());
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "更新商城通知")
    @ApiOperation("更新商城通知")
    @PostMapping(value = "/update/{id}")
    @PreAuthorize("hasAuthority('sys:adminSysNotice:update')")
    public Object updateAdminSysNotice(@RequestBody AdminSysNotice entity) {
        try {
            if (IAdminSysNoticeService.updateById(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("更新商城通知：%s", e.getMessage(), e);
            return new CommonResult().failed(e.getMessage());
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "删除商城通知")
    @ApiOperation("删除商城通知")
    @GetMapping(value = "/delete/{id}")
    @PreAuthorize("hasAuthority('sys:adminSysNotice:delete')")
    public Object deleteAdminSysNotice(@ApiParam("商城通知id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("商城通知id");
            }
            if (IAdminSysNoticeService.removeById(id)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("删除商城通知：%s", e.getMessage(), e);
            return new CommonResult().failed(e.getMessage());
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "给商城通知分配商城通知")
    @ApiOperation("查询商城通知明细")
    @GetMapping(value = "/{id}")
    @PreAuthorize("hasAuthority('sys:adminSysNotice:read')")
    public Object getAdminSysNoticeById(@ApiParam("商城通知id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("商城通知id");
            }
            AdminSysNotice coupon = IAdminSysNoticeService.getById(id);
            return new CommonResult().success(coupon);
        } catch (Exception e) {
            log.error("查询商城通知明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }

    }

    @ApiOperation(value = "批量删除商城通知")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.GET)
    @SysLog(MODULE = "sys", REMARK = "批量删除商城通知")
    @PreAuthorize("hasAuthority('sys:adminSysNotice:delete')")
    public Object deleteBatch(@RequestParam("ids") List
            <Long> ids) {
        boolean count = IAdminSysNoticeService.removeByIds(ids);
        if (count) {
            return new CommonResult().success(count);
        } else {
            return new CommonResult().failed();
        }
    }
    @ApiOperation("修改展示状态")
    @RequestMapping(value = "/update/updateShowStatus")
    @ResponseBody
    @SysLog(MODULE = "cms", REMARK = "修改展示状态")
    public Object updateShowStatus(@RequestParam("ids") Long ids,
                                   @RequestParam("showStatus") Integer showStatus) {
        AdminSysNotice record = new AdminSysNotice();
        record.setStatus(showStatus);
        record.setNoticeId(ids);
        if (IAdminSysNoticeService.updateById(record)) {
            return new CommonResult().success();
        } else {
            return new CommonResult().failed();
        }
    }

    @SysLog(MODULE = "sys", REMARK = "导出社区数据")
    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response, AdminSysNotice entity) {
        // 模拟从数据库获取需要导出的数据
        List<AdminSysNotice> personList = IAdminSysNoticeService.list(new QueryWrapper<>(entity));
        // 导出操作
        EasyPoiUtils.exportExcel(personList, "导出社区数据", "社区数据", AdminSysNotice.class, "导出社区数据.xls", response);

    }

    @SysLog(MODULE = "sys", REMARK = "导入社区数据")
    @PostMapping("/importExcel")
    public void importUsers(@RequestParam MultipartFile file) {
        List<AdminSysNotice> personList = EasyPoiUtils.importExcel(file, AdminSysNotice.class);
        IAdminSysNoticeService.saveBatch(personList);
    }
}


